﻿using System;
using System.Linq;
using System.Windows.Forms;
using Relocation.Data;
using Relocation.Com.Tools;
using Relocation.Service;
using Relocation.Com;
using Relocation.Base;
using Relocation.Base.UI;


namespace Relocation.UI
{
	public partial class UserWindow : BaseForm
	{
		/// <summary>
		/// 密码的最大长度
		/// </summary>
		private const int MaxPassWordLength = 12;

		private User User;
		/// <summary>
		/// 临时唯一密码
		/// </summary>
		private string tempPassWord = Relocation.Com.Encode.MD5.HashString(System.DateTime.Now.ToLongDateString()).Substring(0, MaxPassWordLength);
		/// <summary>
		/// 旧密码
		/// </summary>
		private string oldPassWord;

		public UserWindow()
			: base()
		{
			InitializeComponent();
			Initialize();

		}

		public UserWindow(Session session)
			: base(session)
		{
			InitializeComponent();
			Initialize();
		}

		public UserWindow(Session session, User user)
			: this(session)
		{
			this.User = user;
			InitUser(this.User);
		}

		private void Initialize()
		{
			this.Field_password.Tag = new ControlTag(User.GetPropName(t=>t.password), new ValidatorInfo(true));
			this.Field_name.Tag = new ControlTag(User.GetPropName(t=>t.name), new ValidatorInfo(new ValidatorInfo.ValidatorHandler(delegate(Control control) {
				string value = control.Text;
				if (String.IsNullOrEmpty(value))
				{
					return new ValidatorInfo.ValidatorResult(false, "用户名必须填写");
				}
				int userId = this.Session.DataModel.Users.Where(t => t.name.Equals(value)).Select(t => t.user_id).FirstOrDefault();
				if (userId == 0)
					return new ValidatorInfo.ValidatorResult(true);
				if (this.User != null && this.User.user_id == userId)
					return new ValidatorInfo.ValidatorResult(true);
				else
					return new ValidatorInfo.ValidatorResult(false, "用户名已经存在");

			})));
			this.Field_password.MaxLength = MaxPassWordLength;
		}

		private void InitUser(User user)
		{
			this.Field_name.Text = user.name;
			this.Field_password.Text = this.tempPassWord;
			this.Field_enable.Checked = user.enable == null ? false : user.enable.ToString().Equals("1");
			this.Field_role.Text = user.role.Equals("admin") ? "管理员" : user.role.Equals("operator") ? "操作员" : "普通用户";
			this.Field_remark.Text = user.remark;
			this.oldPassWord = user.password;

		}

		private void save_Click(object sender, EventArgs e)
		{

			try
			{
				if (!this.ValidatorAllControls())
					return;

				if (this.User == null)
				{
					this.User = new User();
					this.User.user_id = DataModel.GenerateID(this.User);
					this.User.created = System.DateTime.Now;
				}
				this.User.name = this.Field_name.Text;
				this.User.password = this.Field_password.Text.Equals(this.tempPassWord) ? this.oldPassWord : Relocation.Com.Encode.MD5.HashString(this.Field_password.Text);
				this.User.enable = this.Field_enable.Checked ? byte.Parse("1") : byte.Parse("0");
				this.User.role = this.Field_role.Text.Equals("管理员") ? "admin" : this.Field_role.Text.Equals("操作员") ? "operator" : "general";
				this.User.updated = DateTime.Now;
				this.User.remark = this.Field_remark.Text;

				this.Session.DataModel.Save(this.User);
				this.FieldIsChanged = false;
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{
				this.Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("用户信息保存失败！");
			}

		}

	}
}
